package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import classes_basicas.Cliente;
import fachada.Fachada;

public class LIstar_Clientes {

	 JFrame frame;
	private JTextField txbfiltronome;
	private JTable table;
Cliente c = new Cliente();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LIstar_Clientes window = new LIstar_Clientes();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
public boolean listando2(Cliente cliente) throws Exception{
		try{
		Fachada f = new Fachada();
		cliente.setNome("");
		ArrayList<Cliente> listc = new ArrayList<>();
		listc= (ArrayList<Cliente>) f.listarcliente(cliente);
		
		DefaultTableModel table2 = new DefaultTableModel();
		table2.getDataVector().removeAllElements();
		table2.setColumnIdentifiers(new String[]{"Id_Cliente", "Nome", "Cpf","Sexo","Telefone","Lougradouro","Bairro","Cidade","Estado" });
	    for (int i = 0; i < listc.size();
	     i++) { table2.addRow(new
	     String[]{(Long.toString(listc.get(i).getId())), listc.get(i).getNome(),listc.get(i).getCpf(),listc.get(i).getSexo(),listc.get(i).getTelefone(),listc.get(i).getEndereco().getLogradouro(),
	    		 listc.get(i).getEndereco().getBairro(),listc.get(i).getEndereco().getCidade(),listc.get(i).getEndereco().getEstado()
	     });
	    
	     }
	     table.setModel(table2);
		return true;
		}catch(Exception e){return false;}
	}
	public boolean listando(Cliente cliente) throws Exception{
		try{
		Fachada f = new Fachada();
		
		ArrayList<Cliente> listc = new ArrayList<>();
		listc= (ArrayList<Cliente>) f.listarcliente(cliente);
		
		DefaultTableModel table2 = new DefaultTableModel();
		table2.getDataVector().removeAllElements();
		table2.setColumnIdentifiers(new String[]{"Id_Cliente", "Nome", "Cpf","Sexo","Telefone","Lougradouro","Bairro","Cidade","Estado" });
	    for (int i = 0; i < listc.size();
	     i++) { table2.addRow(new
	     String[]{(Long.toString(listc.get(i).getId())), listc.get(i).getNome(),listc.get(i).getCpf(),listc.get(i).getSexo(),listc.get(i).getTelefone(),listc.get(i).getEndereco().getLogradouro(),
	    		 listc.get(i).getEndereco().getBairro(),listc.get(i).getEndereco().getCidade(),listc.get(i).getEndereco().getEstado()
	     });
	    
	     }
	     table.setModel(table2);
		return true;
		}catch(Exception e){
			return false;
		}
	}

	/**
	 * Create the application.
	 */
	public LIstar_Clientes() {
		initialize();
		try {
			listando2(c);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 601, 473);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setForeground(new Color(128, 0, 0));
		panel.setBorder(new LineBorder(new Color(128, 0, 0), 2));
		panel.setBackground(new Color(135, 206, 250));
		panel.setBounds(0, 0, 595, 445);
		frame.getContentPane().add(panel);
		
		JLabel lblBuscaDeClientes = new JLabel("Busca de Clientes");
		lblBuscaDeClientes.setForeground(new Color(128, 0, 0));
		lblBuscaDeClientes.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblBuscaDeClientes.setBounds(240, 44, 196, 25);
		panel.add(lblBuscaDeClientes);
		
		JButton button_1 = new JButton("Voltar");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		frame.setVisible(false);
		Menu m = new Menu();
		m.frame.show();
			}
		});
		button_1.setForeground(new Color(128, 0, 0));
		button_1.setBounds(471, 127, 90, 28);
		panel.add(button_1);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(88, 171, 473, 252);
		panel.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
					"Id_Cliente", "Nome", "Cpf", "Sexo", "Telefone", "Logradouro", "Bairro", "Cidade", "Estado"
				}
		));
		scrollPane.setViewportView(table);
		
		txbfiltronome = new JTextField();
		txbfiltronome.setColumns(10);
		txbfiltronome.setBounds(223, 127, 122, 28);
		panel.add(txbfiltronome);
		
		JButton button_2 = new JButton("Buscar");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Cliente h = new Cliente();
				h.setNome(txbfiltronome.getText());
				try {
					listando(h);
				} catch (Exception e2) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(null,e2.getMessage());
				}
			}
		});
		button_2.setForeground(new Color(128, 0, 0));
		button_2.setBounds(371, 127, 90, 28);
		panel.add(button_2);
		
		JLabel label_1 = new JLabel("Pesquisar por nome:");
		label_1.setForeground(new Color(128, 0, 0));
		label_1.setBounds(119, 134, 122, 14);
		panel.add(label_1);
	}

}
